﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using H2.Streams;
using System.IO;

namespace Mystery
{
    public class StartingEquipment
    {
        public const int Size = 156;
        public const int Offset = 296;

        public int StartingGrenades;
        public enum StartingGrenadesEnum
        { 
            None = 0,
            Plasma = 1,
        }

        public short Gametype1;
        public short Gametype2;
        public short Gametype3;
        public short Gametype4;
        public enum GametypeEnum
        {
            None = 0,
            CaptureTheFlag = 1,
            Slayer = 2,
            Oddball = 3,
            KingOfTheHill = 4,
            Race = 5,
            Juggernaught = 7,
            Territories = 8,
            Assault = 9,
            AllTeamGameTypes = 11,
            AllGameTypes = 12,
            AllExceptCTF = 13,
        }

        byte[] Padding = new byte[48];

        public H2.DataTypes.Dependancy Collection1;
        public H2.DataTypes.Dependancy Collection2;

        byte[] Padding2 = new byte[80];

        public void ReadFrom(MapStream stream)
        {
            StartingGrenades = stream.ReadInt32();

            Gametype1 = stream.ReadInt16();
            Gametype2 = stream.ReadInt16();
            Gametype3 = stream.ReadInt16();
            Gametype4 = stream.ReadInt16();

            Padding = stream.ReadBytes(Padding.Length);

            Collection1 = stream.ReadDependancy();
            Collection2 = stream.ReadDependancy();

            Padding2 = stream.ReadBytes(Padding2.Length);
        }

        public void WriteTo(BaseStream stream)
        {
            stream.Write(StartingGrenades);

            stream.Write(Gametype1);
            stream.Write(Gametype2);
            stream.Write(Gametype3);
            stream.Write(Gametype4);

            stream.Write(Padding);

            if (Collection1 != null)
                stream.Write(Collection1.Class);
            else stream.Write("itmc");
            stream.Write(-1);

            if(Collection2 != null)
                stream.Write(Collection2.Class);
            else stream.Write("itmc");
            stream.Write(-1);

            stream.Write(Padding2);
        }

        public static void Generate(Scenario scnr, Palette[] palette)
        {
            StartingEquipment se = new StartingEquipment();
            scnr.StartingEquipment.Add(se);

            se.Gametype1 = (short)GametypeEnum.AllGameTypes;

            foreach(Palette p in palette)
            {
                if (Path.GetFileName(p.Item.Path) == "smg")
                {
                    se.Collection1 = new H2.DataTypes.Dependancy();
                    se.Collection1.ClassString = "itmc";
                    se.Collection1.Path = "multiplayer\\single_weapons\\smg";
                    break; //Prefer smg
                }

                if (Path.GetFileName(p.Item.Path) == "plasma_rifle")
                {
                    se.Collection1 = new H2.DataTypes.Dependancy();
                    se.Collection1.ClassString = "itmc";
                    se.Collection1.Path = "multiplayer\\single_weapons\\plasma_rifle";
                }
            }
        }
    }
}
